﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace App_Log
{
    public class clsLog
    {
        public static string _sLogFile
        {
            get { return sLogFile; }
            set { sLogFile = value; }
        }

        public static string _sLogDirectory
        {
            get { return sLogDirectory; }
            set { sLogDirectory = value; }
        }

        public static string _sBackupLogDirectory
        {
            get { return sBackupLogDirectory; }
            set { sBackupLogDirectory = value; }
        }

        private static string sLogFile = "Logs_" + string.Format("{0:MM_dd_yyyy}", DateTime.Now) + ".log";
        private static string sLogDirectory =  "Log";
        private static string sBackupLogDirectory = "BackUpLog";

        public void logdirectory(string sLogDirectory)
        {
            _sLogDirectory = sLogDirectory;
        }

        public static void backuplogdirectory(string sBackupLogDirectory)
        {
            _sBackupLogDirectory = sBackupLogDirectory;
        }

        private static void log(string sLogStr)
        {
            try
            {
                if (!Directory.Exists(_sLogDirectory))
                {
                    Directory.CreateDirectory(_sLogDirectory);
                }

                TextWriter tw = new StreamWriter(_sLogDirectory + "\\" + _sLogFile, true);
                tw.WriteLine(DateTime.Now + " - " + sLogStr);
                tw.Close();
            }
            catch { }
        }

        public static void logError(string moduleName, string functionName, string errorMessage)
        {
            log("Error in Module: " + moduleName + " Function:" + functionName + ": " + errorMessage);
        }

        public static void logMsg(string moduleName, string functionName, string logMessage)
        {
            log("Module: " + moduleName + " Function:" + functionName + ": " + logMessage);
        }

        public static void backupLogFile(string sLogFile)
        {
            try
            {
                FileInfo fi = new FileInfo(_sLogDirectory + "\\" + sLogFile);
                if (fi.Exists)
                {
                    if (fi.LastWriteTime.Day != DateTime.Now.Day || fi.LastWriteTime.Month != DateTime.Now.Month || fi.LastWriteTime.Year != DateTime.Now.Year)
                    {
                        //delete the existing backup file if it exists
                        string sBackupName = _sBackupLogDirectory + "\\" + sLogFile + "." + (DateTime.Now.Day).ToString();
                        FileInfo fi_old = new FileInfo(sBackupName);
                        if (fi_old.Exists) { fi_old.Delete(); }

                        //move the file to the backup directory.
                        fi.MoveTo(sBackupName);

                        fi = null;
                        fi_old = null;
                    }
                }
            }
            catch (Exception err)
            {
                log("Error moving to backup location: " + err.Message);
                //continue on if the move backup fails.
            }
        }

    }

}
